package com.hubei.control;

import com.hubei.service.UserService;
import com.hubei.vo.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@ 郭铭文  //

@WebServlet("/login.action")
public class LoginControl extends HttpServlet {
    private UserService userService = UserService.getInstance();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求的编码格式为UTF-8
        request.setCharacterEncoding("UTF-8");
        // 设置响应的编码格式为UTF-8
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 打印用户名和密码（用于调试）
        System.out.println("用户名：" + username);
        System.out.println("密码：" + password);



        Users users = new Users(username, password);
        Users result = userService.userLogin(users);

        if (result != null) {
            request.getSession().setAttribute("userInfo", result);
            response.sendRedirect("./main.jsp");
        } else {

            request.setAttribute("loginErr", "用户名或密码错误");
            request.getRequestDispatcher("./login.jsp").forward(request, response);
        }
    }
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}